package com.moon.game.command.guest.lair;

import java.util.ArrayList;
import java.util.List;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.manager.AnimalMG;
import com.moon.game.resource.model.Animal;
import com.moon.game.service.ChatService;
import com.moon.game.service.LairService;
import com.moon.game.service.PlayerService;
import com.moon.game.util.GDateUtils;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;
import com.moon.game.vos.PlayerVo.Tower;

/**
 * @description 进入孵化塔
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-7-12 下午3:55:05
 */
public class EnterLairCmd extends BaseCmd{

	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doCheckSelf(req);
			break;
		case STATU_RETURN:
			doResponse(req);
			break;
		default:
			break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null) {
			doResponsePsError(req);
			return;
		}
		PlayerVo player = req.getPlayer();
		List<Tower> towers = player.getTowerList();
		if (towers == null) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_TOWER, "进入孵化塔出错，你当前没有孵化塔！"));
			return;
		}
		Integer towerId = Integer.parseInt(ps[0]);
		if (towerId == null) {
			doResponsePsError(req);
			return;
		}
		Tower theTower = LairService.findLair(towers, towerId);
		//没有指定孵化塔
		if (theTower == null) {
			//清除修理消息
			ChatService.removeThePrivateActMessage(req.getPlayerId(), Constants.REPAIR_LAIR, String.valueOf(towerId));
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_TOWER, "你当前没有指定的孵化塔！"));
			return;
		}
		//检测自然侵蚀
		int erode = LairService.calcMinusLairEndure(2, GDateUtils.now() - theTower.getLastShowTime());
		if (erode > theTower.getCurrEndure()) {
			List<Tower> newTowerList = new ArrayList<Tower>();
			for (Tower tower : towers) {
				if (tower.getTowerId() == towerId) {
					continue;
				}
				newTowerList.add(tower);
			}
			player.setTowerList(newTowerList);
			PlayerService.doUpdatePlayer(player.getSid(), player.getId(), player);
			String info = "你的孵化塔因自然侵蚀到持久底线已经导致倒塌，塔内珍兽在孵化塔倒塌时已经离开！";
			req.setVO(TAG_INFO, info);
			//清除修理消息
			ChatService.removeThePrivateActMessage(req.getPlayerId(), Constants.REPAIR_LAIR, String.valueOf(towerId));
			//返回
			response(req, req.getClientResponse());
			return;
		}
		//持久降低
		theTower.setCurrEndure(theTower.getCurrEndure() - erode);
		Animal animal = null;
		boolean needRefreash = false;
		switch(theTower.getHierarchy()) {
			case 0:
				if (theTower.getAnimalCalledTime() <= GDateUtils.now() - Constants.COMMON_FREASH_TIME) {
					needRefreash = true;
				}
				break;
			case 1:
				if (theTower.getAnimalCalledTime() <= GDateUtils.now() - Constants.IMMORTAL_FREASH_TIME) {
					needRefreash = true;
				}
				break;
			case 2:
				if (theTower.getAnimalCalledTime() <= GDateUtils.now() - Constants.GOD_FREASH_TIME) {
					needRefreash = true;
				}
				break;
		}
		if (theTower.getAnimalCalledTime() == 0 || theTower.getAnimalId() == 0) {
			needRefreash = true;
		}
		
		if (needRefreash) {
			animal = LairService.RefreashAnimal(theTower);
		}else {
			animal = AnimalMG.instance.getAnimal(theTower.getAnimalId());
		}
		
		int leftRefreashTime = 0;//下次刷新的时间
		switch(theTower.getHierarchy()) {
			case 0:
				leftRefreashTime = theTower.getAnimalCalledTime() + Constants.COMMON_FREASH_TIME - GDateUtils.now();
				break;
			case 1:
				leftRefreashTime = theTower.getAnimalCalledTime() + Constants.IMMORTAL_FREASH_TIME - GDateUtils.now();
				break;
			case 2:
				leftRefreashTime = theTower.getAnimalCalledTime() + Constants.GOD_FREASH_TIME - GDateUtils.now();
				break;
		}
		req.setVO("animal", animal);
		req.setVO("tower", theTower);
		req.setVO("destory_price", Constants.DESTORY_LAIR_PRICE);
		req.setVO("incubation_price", Constants.INCUBATION_PRICE);
		req.setVO("call_price", Constants.CALL_ANIMAL_PRICE);
		req.setVO("psychedelic_price", Constants.PSYCHEDELIC_ANIMAL_PRICE);
		String refreashInfo = "珍兽将在" + GDateUtils.formatSeconds(leftRefreashTime) + "之后离开，并有新的珍兽驻足！";
		req.setVO("refreash_info", refreashInfo);
		theTower.setLastShowTime(GDateUtils.now());
		//返回
		response(req, req.getClientResponse());
	}
}
